package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setMealMapper;

    @Autowired
    private SetmealDishMapper setMealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    /**
     * 修改套餐，同时更新套餐和菜品的关联关系
     * @param setmealDTO
     */
    public void updateWithDish(SetmealDTO setmealDTO) {
        // Implementation of the update logic goes here
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        setMealMapper.update(setmeal);

        setMealDishMapper.deleteBySetmealId(setmealDTO.getId());

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealDTO.getId());
        });
        setMealDishMapper.insertBatch(setmealDTO.getSetmealDishes());
    }

    /**
     * 根据id查询套餐信息和对应的菜品信息
     * @param id
     * @return
     */
    public SetmealVO getByIdWithDishes(Long id) {
        // Implementation of the get logic goes here
        Setmeal setmeal = setMealMapper.getById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);

        List<SetmealDish> setmealDishes = setMealDishMapper.getBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 分页查询套餐信息
     * @param setmealPageQueryDTO
     * @return
     */
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO){
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());

        Page<SetmealVO> page =  setMealMapper.page(setmealPageQueryDTO);

        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 新增套餐，同时保存套餐和菜品的关联关系
     * @param setmealDTO
     */
    public void saveWithDish(SetmealDTO setmealDTO){
       Setmeal setmeal = new Setmeal();
       BeanUtils.copyProperties(setmealDTO, setmeal);
       setMealMapper.insert(setmeal);
       Long setmealId = setmeal.getId();

       List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
       if(setmealDishes != null && setmealDishes.size() > 0){
           setmealDishes.forEach(setmealDish -> {
              setmealDish.setSetmealId(setmealId);
           });
       }

       setMealDishMapper.insertBatch(setmealDishes);
    }

    /**
     * 起售/停售套餐
     * @param status
     * @param id
     */
    public void updateStatus(Integer status, Long id){
        List<SetmealDish> setmealDishes = setMealDishMapper.getBySetmealId(id);
        for(SetmealDish setmealDish : setmealDishes){
            Dish dish = dishMapper.getById(setmealDish.getDishId());
            if(dish.getStatus() == StatusConstant.DISABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);
        setMealMapper.update(setmeal);
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    public void deleteBatch(List<Long> ids) {
       for(Long id : ids){
           if(setMealMapper.getById(id).getStatus() == 1){
               throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
           }
       }

       setMealMapper.deleteBatch(ids);

       setMealDishMapper.deleteBySetmealIds(ids);
    }

    /**
     * 根据条件查询套餐列表
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setMealMapper.list(setmeal);
        return list;
    }

    public List<DishItemVO> getDishItemBySetmealId(Long id) {
        return setMealDishMapper.getDishItemBySetmealId(id);
    }
}
